package com.shaoshaohuo.app.view;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.adapter.DialogListViewAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/30.
 */

public class ApplyBackDialog extends Dialog {
    private List<String> data;
    private DialogListViewAdapter adapter;
    private ListView list;
    private Button button;
    private int selectPosition = -1;

    public ApplyBackDialog(@NonNull Context context) {
        super(context, R.style.CustomDialog);
        init();
        getReasonData();
        bindAdapter();
        initListener();
    }

    private dialogListener listener;

    public void setListener(dialogListener listener) {
        this.listener = listener;
    }

    public interface dialogListener {
        void listItemListener(int position, List<String> data);
    }

    private void initListener() {
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.setSelectPosition(position);
                listener.listItemListener(position, data);
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }

    private void bindAdapter() {
        adapter = new DialogListViewAdapter(getContext(), data);
        list.setAdapter(adapter);
    }

    private void getReasonData() {
        data.add("商品包装破损");
        data.add("商品变质/过期,质量不好");
        data.add("拍错了,不想要");
        data.add("发货慢/物流慢");
    }

    private void init() {
        setContentView(R.layout.apapply_back_money_dialog);
        list = (ListView) findViewById(R.id.list);
        button = (Button) findViewById(R.id.button);
        data = new ArrayList<>();
        Window window = getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.width = getContext().getResources().getDisplayMetrics().widthPixels;
        attributes.height = getContext().getResources().getDisplayMetrics().heightPixels / 2;
        attributes.dimAmount = 0.5f;
        attributes.gravity = Gravity.BOTTOM;
        window.setAttributes(attributes);
        setCanceledOnTouchOutside(true);
    }


}
